package com.yyt_study.xf_voice;

import java.util.HashMap;
import java.util.LinkedHashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechUtility;
import com.yyt_study.xf_voice.util.JsonParser;

public class MainActivity extends Activity implements OnClickListener, RecognizerListener {
	private TextView tvWord;
	private Button btnVoice;
	SpeechRecognizer mIat;
	// 用HashMap存储听写结果
	private HashMap<String, String> mIatResults = new LinkedHashMap<String, String>();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SpeechUtility.createUtility(this, SpeechConstant.APPID + "=556d7bc3"); // 初始化讯飞语音对象
		initView();
		initXFVoice();
	}

	
	/**
	  * @Title: initView
	  * @Description: 初始化布局控件
	  * @param     设定文件
	  * @author username
	  * @return void    返回类型
	  */
	
	private void initView()
	{

		tvWord = (TextView) findViewById(R.id.tv_word);
		btnVoice = (Button) findViewById(R.id.btn_voice);
		btnVoice.setOnClickListener(this);
	}

	
	/**
	  * @Title: initXFVoice
	  * @Description: 初始化语言的基本参数
	  * @param     设定文件
	  * @author username
	  * @return void    返回类型
	  */
	
	private void initXFVoice()
	{
		// 1.创建SpeechRecognizer对
		mIat = SpeechRecognizer.createRecognizer(this, null);
		// 2.设置听写参数
		//设置应用领域
		mIat.setParameter(SpeechConstant.DOMAIN, "iat");
		//设置语言为中文
		mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
		//设置语言区域
		mIat.setParameter(SpeechConstant.ACCENT, "mandarin ");
		// 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
		mIat.setParameter(SpeechConstant.ASR_PTT, "0");

	}

	@Override
	public void onBeginOfSpeech()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onEndOfSpeech()
	{
		Log.i("info", "onEndOfSpeech");
	}

	@Override
	public void onError(SpeechError error)
	{
		Log.i("info", "onError" + error.getPlainDescription(true));
	}

	@Override
	public void onEvent(int arg0, int arg1, int arg2, Bundle arg3)
	{
		Log.i("info", "onEvent");
	}

	@Override
	public void onResult(RecognizerResult result, boolean isLast)
	{
		String sn = null;
		if (result != null) {
			//调用utli的jsonParser的功能，打印出数据
			String text = JsonParser.parseIatResult(result.getResultString());

			JSONObject jsonObjResult;
			try {
				jsonObjResult = new JSONObject(result.getResultString());
				sn = jsonObjResult.optString("sn");
			} catch (JSONException e) {
				// TODO Auto-generated catch block

				e.printStackTrace();
			}
			mIatResults.put(sn, text);
			StringBuffer resultBuffer = new StringBuffer();
			for (String key : mIatResults.keySet()) {
				resultBuffer.append(mIatResults.get(key));
			}

			tvWord.setText(resultBuffer.toString());
			//调用utli的jsonParser的功能，打印出数据 End
		}

	}

	@Override
	public void onVolumeChanged(int arg0)
	{
		// Log.i("info", "onVolumeChange");
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.btn_voice:
			mIat.startListening(this);
			break;

		default:
			break;
		}

	}

}
